package com.dmjj.limit.controller;

import com.dmjj.common.base.BaseController;
import com.dmjj.common.result.ResultEnum;
import com.dmjj.common.result.ResultObject;
import com.dmjj.common.util.ListCopyToVOUtil;
import com.dmjj.common.util.PaginationVOUtil;
import com.dmjj.common.vo.ResultVO;
import com.dmjj.limit.dto.DataAuthDTO;
import com.dmjj.limit.dto.UserAuthDTO;
import com.dmjj.limit.service.DataAuthService;
import com.dmjj.limit.vo.DataAuthVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 数据范围授权表控制器
 * @Author zhangxue
 * @Date 2019-03-23 15:00:15
 */

@RestController
@Api(tags = "数据范围授权表控制器")
@RequestMapping("dataAuth")
public class DataAuthController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(DataAuthController.class);

    @Autowired
    private DataAuthService service;
    @Autowired
    private ResultObject resultObject;

    /**
	 * 数据范围授权表列表
     * @Author zhangxue
     * @Date 2019-03-23 15:00:15
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "数据范围授权表列表", response = DataAuthVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            DataAuthVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            DataAuthDTO entityDTO = new DataAuthDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<DataAuthDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, DataAuthVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 数据范围授权表列表（无分页）
     * @Author zhangxue
     * @Date 2019-03-23 15:00:15
     * @Param
     * @Return
     */
    @ApiOperation(value = "数据范围授权表列表（无分页）", response = DataAuthVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(DataAuthVO entity){
        try {
            DataAuthDTO entityDTO = new DataAuthDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<DataAuthDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, DataAuthVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 数据范围授权表详细信息
     * @Author zhangxue
     * @Date 2019-03-23 15:00:15
     * @Param
     * @Return
     */
    @ApiOperation(value = "数据范围授权表详细信息", response = DataAuthVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            DataAuthDTO entityDTO = service.queryOneByPk(id);
            DataAuthVO entity = new DataAuthVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存数据范围授权表信息
     * @Author zhangxue
     * @Date 2019-03-23 15:00:15
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存数据范围授权表信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(DataAuthVO entity) {
        try {
            DataAuthDTO entityDTO = new DataAuthDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            //查询用户是否已经授权权限范围
            List<DataAuthDTO> IsExistList = service.queryDataAuthIsExist(entityDTO.getUserno());
            if(IsExistList.size()>0){
                if(service.edit(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }

            }else{
                if(service.save(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除数据范围授权表信息
     * @Author zhangxue
     * @Date 2019-03-23 15:00:15
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除数据范围授权表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}